package org.snail.employee.controller;

import org.snail.common.dingding.utils.JedisUtils;
import org.snail.constant.CacheNameConstant;
import org.snail.constant.ErrCodeConstant;
import org.snail.employee.bean.EmployeesDO;
import org.snail.employee.service.impl.EmployeesServiceImpl;
import org.snail.exception.BaseException;
import org.snail.exception.CommonException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Test {

    @Autowired
    private EmployeesServiceImpl employeesService;
    @Autowired
    private JedisUtils jedisUtils;

//    @RequestMapping("/redisTest")
//    @ResponseBody
//    public Long testRedis(EmpCondition empCondition){
//        return redisTestService.findAllEmp();
//    }

    @RequestMapping("/save2Redis")
    @ResponseBody
    public Integer save2Redis() throws BaseException {
        return employeesService.saveId2empDOHCache();
    }

    @RequestMapping("/getFromRedis")
    @ResponseBody
    public EmployeesDO getFromRedis(String hashKey) throws BaseException {
        if (StringUtils.isEmpty(hashKey)){
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "参数不能为空");
        }
        return employeesService.getEmpDOFromHCache(hashKey);
    }

    @RequestMapping("/existHashKey")
    @ResponseBody
    public boolean existHashKey(String hashKey) throws BaseException {
        if (StringUtils.isEmpty(hashKey)){
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "参数不能为空");
        }
        return jedisUtils.isKeyExist(CacheNameConstant.EMPID_EMPDO_HASH,hashKey);
    }

    @RequestMapping("/delRedisKey")
    @ResponseBody
    public Long delRedisKey(String hashKey) throws BaseException {
        if (StringUtils.isEmpty(hashKey)){
            throw new CommonException(ErrCodeConstant.ILLEGAL_PARAM, "参数不能为空");
        }
        return jedisUtils.deleteKey(CacheNameConstant.EMPID_EMPDO_HASH, hashKey);
    }

    @RequestMapping("/getCacheSize")
    @ResponseBody
    public Long getCacheSize() throws BaseException {

        return jedisUtils.getCacheSize(CacheNameConstant.EMPID_EMPDO_HASH);
    }


}
